﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InformationSystem.BLL.Core.Service;
using InformationSystem.BLL.Core.Model;
using Ext.Net;
using System.IO;

namespace InformationSystem.WebSite.Module.DocumentMgr
{
    public partial class DocumentUpload : BasePage
    {
        public ISharedDocumentService SharedDocumentService { get; set; }
        LoginUser user = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            user = (LoginUser)Session["LoginUser"];
            if (!X.IsAjaxRequest && !IsPostBack)
            {
                if (!this.CommitPermissionCheck())
                {
                    Response.ClearContent();
                    X.Msg.Show(new MessageBoxConfig
                    {
                        Buttons = MessageBox.Button.OK,
                        Icon = MessageBox.Icon.INFO,
                        Title = "信息",
                        Message = "你没有操作权限,请联系管理员！"
                    });
                    return;
                }
            }
        }
        protected void btnUpload_Click(object sender, DirectEventArgs e)
        {
            if (!this.CommitPermissionCheck())
            {
                Response.ClearContent();
                X.Msg.Show(new MessageBoxConfig
                {
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.INFO,
                    Title = "信息",
                    Message = "你没有操作权限,请联系管理员！"
                });
                return;
            }

            if (!this.FileUpload.HasFile)
            {
                X.Msg.Show(new MessageBoxConfig
                {
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.INFO,
                    Title = "信息",
                    Message = "请选择需要上传的文件！"
                });
                return;
            }
            string savepath = Path.Combine(Server.MapPath("~"), Common.DocumentPath, user.CollegeID.ToString(), user.GradeID.ToString());
            if (!Directory.Exists(savepath))
            {
                Directory.CreateDirectory(savepath);
            }

            FileInformation fin = new FileInformation();
            fin.UploadFileName = Path.GetFileName(this.FileUpload.PostedFile.FileName);
            fin.SaveFileName = Guid.NewGuid().ToString() + Path.GetExtension(this.FileUpload.PostedFile.FileName);
            fin.CreateUserID = user.UserID;
            fin.CreateUserName = user.UserName;
            fin.CollegeId = user.CollegeID;
            fin.GradeId = user.GradeID;
            fin.IsPublicToCollege = false;
            fin.CreateTime = DateTime.Now;

            this.FileUpload.PostedFile.SaveAs(Path.Combine(savepath, fin.SaveFileName));

            bool result = SharedDocumentService.CreateDocument(fin);

            if (result)
            {
                X.Msg.Show(new MessageBoxConfig
                {
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.INFO,
                    Title = "信息",
                    Message = "上传成功！"
                });
                baseForm.Reset();
                return;
            }
            else
            {
                X.Msg.Show(new MessageBoxConfig
                {
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.INFO,
                    Title = "信息",
                    Message = "上传失败！"
                });
                return;
            }
        }


        protected override bool CommitPermissionCheck()
        {
            if (user.LoginUserType == UserType.Guest || user.LoginUserType == UserType.Student)
            {
                return false;
            }
            return true;
        }

    }
}